<template>
	<view class="tabbar" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}">
		<view class="tabbar-item" v-for="(item, index) in list" :key="index" @click="tabbarChange(item.pagePath)">
			<view class="flexleft huibiao" v-if="index==3">
				<u-badge max="99" :value="noreadNum"></u-badge>
			</view>
			<image class="item-img" :src="item.selectedIconPath" v-if="current == index"></image>
			<image class="item-img" :src="item.iconPath" v-else></image>
			<view class="item-name" :class="{'tabbarActive': current == index}" v-if="item.text">{{item.text}}
			</view>
		</view>
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		name: "tabbar",
		components: {},
		props: ['current'],
		data() {
			return {
				paddingBottomHeight: 0, //苹果X以上手机底部适配高度
				list: [{
						"text": "首页",
						"pagePath": "/pages/homepage",
						"iconPath": "/static/image/tabbar/shouyes.png",
						"selectedIconPath": "/static/image/tabbar/shouye.png"
					}, {
						"text": "短视频",
						"pagePath": "/pages/video",
						"iconPath": "/static/image/tabbar/shipins.png",
						"selectedIconPath": "/static/image/tabbar/shipin.png"
					},
					{
						"text": "发布",
						"pagePath": "/pages/fabu",
						
						"iconPath": "/static/image/tabbar/fabu.png",
						"selectedIconPath": "/static/image/tabbar/fabus.png"
					}, {
						"text": "消息",
						"pagePath": "/pages/message",
						"iconPath": "/static/image/tabbar/xiaoxis.png",
						"selectedIconPath": "/static/image/tabbar/xiaoxi.png"
					},
					{
						"text": "我的",
						"pagePath": "/pages/mypage",
						"iconPath": "/static/image/tabbar/wodes.png",
						"selectedIconPath": "/static/image/tabbar/wode.png"
					}
				],
				noreadNum:0
			}
		},
		mounted() {
			uni.$on("updatechatFnteList", data => {
				console.log("别人给我发消息触发方法了");
				this.getnoreadNum()
			})
		},
		destroyed() {
			uni.$off('updatechatFnteList')
		},
		created() {
			let that = this;
			uni.getSystemInfo({
				success: function(res) {
					let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
					model.forEach(item => {
						//适配iphoneX以上的底部，给tabbar一定高度的padding-bottom
						if (res.model.indexOf(item) != -1 && res.model.indexOf('iPhone') != -1) {
							that.paddingBottomHeight = 0;
						}
					})
				}
			});
		},
		methods: {
			getnoreadNum(){
				if (!httpRequest.checkIsLogin()) {
					return false;
				}
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.noreadNum = res.data.total_noread_num
				})
			},
			tabbarChange(path) {
				if (!httpRequest.checkIsLogin()) {
					if(path=='/pages/message'){
						uni.navigateTo({
							url:"/pages/login/login"
						})
						return false
					}
				}
				uni.switchTab({
					url: path
				})
			}
		}

	}
</script>

<style lang="scss">
	.huibiao{
		position: absolute;
		right: 5rpx;
		top: 0rpx;
	}
	.item-img {
		height: 40rpx;
		width: 40rpx;
	}

	.tabbar {
		display: flex;
		justify-content: space-around;
		align-items: flex-end;
		flex-direction: row;
		background: #100d1f;
		border-radius: 0rpx;
		/* #ifdef MP-WEIXIN */
		height: 140rpx;
		/* #endif */
		/* #ifdef H5 */
		height: 140rpx;
		/* #endif */
		/* #ifdef APP-PLUS */
		height: 140rpx;
		/* #endif */
		padding: 0 30rpx;
		box-sizing: border-box;
		position: fixed;
		width: 750rpx;
		bottom: 0;
		overflow: inherit;
		z-index: 100;
		// box-shadow: 0px -2px 4px 1px rgba(0, 0, 0, 0.05);
	}

	.tabbar-item {
		margin-bottom: 30rpx;
		width: 138rpx;
		display: flex;
		align-items: center;
		flex-direction: column;
		position: relative;
	}

	.item-name {
		font-size: 24rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		text-align: center;
		color: #5A5B6D;
		margin-top: 14rpx;
	}

	.tabbarActive {
		color: #DD5056;
		text-align: center;
	}
</style>